home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / polyval < prev    next >
Text File  |  1996-04-09  |  2KB  |  107 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax: polyval(p, x), polyvalm(p, X)
  4.  
  5. //  Description:
  6.  
  7. //  Polynomial evaluation
  8. //
  9. //  p is a vector (colum or row) whose elements are the coefficients 
  10. //  of a polynomial in decending powers.
  11. //
  12. //  polyval evaluates and returns
  13. //      t = p[1]*x.^n + p[2]*x.^(n-1) + ... + p[n+1]
  14. //  x can be a vector (row or column) or a matrix.
  15. //
  16. //  polyvalm evaluates and returns
  17. //      t = p[1]*x^n + p[2]*x^(n-1) + ... + p[n+1]
  18. //  X must be a square matrix.
  19.  
  20.  
  21. //  Example 1:  Evaluate 2*x^2 + x + 5 at points x=1,2,3
  22. //  > p=[2,1,5]
  23. //   p =
  24. //          2          1          5
  25. //  > x=[1,2,3]
  26. //   x =
  27. //          1          2          3
  28. //  > polyval(p,x)
  29. //          8         15         26
  30. //
  31. //  Example 2:  Evaluate 2*x^2 + x + 5 at its roots
  32. //  > p=[2,1,5]
  33. //   p =
  34. //          2          1          5
  35. //  > r=roots(p)
  36. //   r =
  37. //             -0.25 - 1.56i
  38. //             -0.25 + 1.56i
  39. //  > polyval(p,r)
  40. //          0
  41. //          0
  42. //  
  43. //  Example 3: Test Cayley-Hamilton theorem
  44. //  > X=[1,-2;-2,1];
  45. //  > p=poly(X)
  46. //   p =
  47. //          1         -2         -3
  48. //  > polyvalm(p,X)
  49. //          0          0
  50. //          0          0
  51. //
  52.  
  53. //  See Also: poly, polyfit, roots
  54. //
  55. //-------------------------------------------------------------------//
  56.  
  57. polyval = function (p, x)
  58. {
  59.    if (class(p) == "num") {   
  60.       if (p.n <= 0) {
  61.          error("The first argument of polyval() is null.");
  62.       else if (min(size(p)) != 1) {
  63.          error("The first argument of polyval() must be a column or a row vector.");
  64.       }}
  65.  
  66.       // scalar polynomial 
  67.       if (p.n == 1) {
  68.          return p;
  69.       }     
  70.       t = p[1]*ones(size(x));
  71.       for(i in 2:p.n)
  72.       {
  73.         t = t.*x + p[i];
  74.       }
  75.    else {
  76.      error("Wrong argument class in polyval().");
  77.    }}
  78.    return t;
  79. };
  80.  
  81. polyvalm = function (p, x)
  82. {
  83.    if (x.nr != x.nc) {
  84.       error("The 2nd argument of polyvalm() must be a square matrix.");
  85.    }
  86.    if (class(p) == "num") {   
  87.       if (p.n <= 0) {
  88.          error("The first argument of polyvalm() is null.");
  89.       else if (min(size(p)) != 1) {
  90.          error("The first argument of polyvalm() must be a column or a row vector.");
  91.       }}
  92.  
  93.       // scalar polynomial 
  94.       if (p.n == 1) {
  95.          return p;
  96.       }     
  97.       s = p[1]*ones(size(x));
  98.       for(i in 2:p.n)
  99.       {
  100.         s = s*x + p[i]; 
  101.       }
  102.    else {
  103.      error("Wrong argument class in polyvalm().");
  104.    }}
  105.    return s;
  106. };
  107.